/*
 * 此页面主要是注意businessOpportunitiesForm 这个变量是根据businessOpportunitiesView.jsp页面来的
 * */
Ext.define('App.crm.salesOfProductsList', {
    extend      : 'Ext.panel.Panel',
    alias       : 'widget.salesOfProductsList',
    layout:'fit',
    autoScroll:true,
    autoHeight:true,
    initComponent : function() {
    	var grid,   searchFlds;
    		searchFlds = new Ext.util.MixedCollection();
    		
        var	toolbar = new Ext.Toolbar({
            enableOverflow : true,
            items : [ {
                text : "新增",
                iconCls : 'addIcon',
                handler : function() {
                    var  salesOfProductsForm = Ext.create('App.crm.salesOfProductsForm');
                    salesOfProductsForm.show();
                    salesOfProductsForm.setTitle('新增');
                    if(businessOpportunitiesForm){
                    salesOfProductsForm.down('form').getForm().findField('businessOpportunitiesName').setValue( businessOpportunitiesForm.getForm().findField("bussName").getValue());
                    salesOfProductsForm.down('form').getForm().findField('businessOpportunitiesId').setValue( businessOpportunitiesForm.getForm().findField("id").getValue());
                }
                   
                }
            }, '-', {
                text : "编辑",
                iconCls : 'editIcon',
                handler : function() {
                    var sm = Ext.getCmp('salesOfProductsGrid').getSelectionModel();
                    var records = sm.getSelection();
                    if (records.length <= 0) {
                        Ext.Msg.alert('错误', "未选中记录，不能修改！");
                        return;
                    }
                    var  salesOfProductsForm = Ext.create('App.crm.salesOfProductsForm');
                    salesOfProductsForm.show();
                    salesOfProductsForm.setTitle('编辑');
                    var data = records[0].raw;
                    salesOfProductsForm.down('form').getForm().loadRecord(records[0]);
                    salesOfProductsForm.down('form').getForm().findField('productName').setValue(data.product.name);
                    salesOfProductsForm.down('form').getForm().findField('productId').setValue(data.product.id);
                    salesOfProductsForm.down('form').getForm().findField('businessOpportunitiesName').setValue(data.businessOpportunities.bussName);
                    salesOfProductsForm.down('form').getForm().findField('businessOpportunitiesId').setValue(data.businessOpportunities.id);
                    if(data.salesDate!=null){
                        salesOfProductsForm.down('form').getForm().findField('salesDate').setValue( new Date(data.salesDate).format('yyyy-MM-dd'));
                }}
            }, '-', {
                text : "删除",
                iconCls : 'deleteIcon',
                handler : function() {
                    Ext.getCmp('salesOfProductsGrid').deleteRecord();
                }
            }, "->",  , {
                text: "搜索",
                iconCls : 'searchIcon',
                handler: function () {
                    grid.load();
                }
            } ]
        });
        grid =	Ext.create('gridView',{
            remoteSort : true,
            forceFit: true,
            gridId :  'salesOfProductsGrid',
            checkboxSelectionModel:true,
            searchFlds : searchFlds,
            dataAutoLoad : false,
            entityClassName : 'com.rover.crm.entity.SalesOfProducts',
            queryUrl : 'salesOfProductsController/query.action',
            deleteUrl : 'salesOfProductsController/delete.action',
            recordPk : [ "id" ],
            tbar:toolbar
        });
        //如果from存在，说明是在businessOpportunitiesView.jsp中打开的
        if(businessOpportunitiesForm)
        	{
        	grid.getStore().on('beforeload', function(store, options) {
    			store.proxy.extraParams.businessOpportunitiesId = businessOpportunitiesForm.getForm().findField("id").getValue();
    				}, this, true);
        	}
        
        Ext.apply(this, {items:[grid]});
        grid.load();
        this.callParent(arguments);
    } , destroy: function () {
        Ext.getCmp('salesOfProductsList').destroy();
       this.destroy();
    }
});